<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 公告管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-lx-add" @click="addBanner">添加</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header">
                <el-table-column prop="title" label="标题" width="120" align="center"></el-table-column>
                <el-table-column label="内容">
                    <template slot-scope="scope">
                        {{scope.row.content}}
                    </template>
                </el-table-column>
                <el-table-column label="是否显示" align="center" width="120">
                    <template slot-scope="scope">
                        {{scope.row.isHide=='1'?'显示':'隐藏'}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            class="mid"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red mid"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="tableData.length"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog :title="title" :visible.sync="editVisible" width="1000px" >
            <el-form ref="form" :model="editForm" label-width="70px">
                <el-form-item label="标题">
                    <el-input v-model="editForm.title"></el-input>
                </el-form-item>
                 <el-form-item label="是否显示">
                    <el-radio v-model="editForm.isHide" label="1">显示</el-radio>
                    <el-radio v-model="editForm.isHide" label="2">隐藏</el-radio>
                </el-form-item>
                <el-form-item label="公告标题">
                    <mavon-editor v-model="editForm.content" ref="md" @imgAdd="$imgAdd" @change="change22" style="min-height: 400px"/>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
import { mavonEditor } from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
export default {
    name: 'banner',
    data() {
        return {
            query: {
                pageNum: 1,
                pageSize: 10
            },
            title:'',
            tableData: [],
            editVisible: false,
            editForm:{},
            roleId:'',
            pageTotal: 0,
            html:'',
            configs: {}
        };
    },
    components: {
        mavonEditor
    },
    created() {
        this.getData();
    },
    methods: {
        // 将图片上传到服务器，返回地址替换到md中
        $imgAdd(pos, $file){
            var formdata = new FormData();
            formdata.append('file', $file);
            console.log(formdata);
            this.$http.postData('hgz/resource/upload',formdata).then(res =>{
                if(res.status){
                    //上传成功
                    this.$message.success("上传成功");
                    var path = res.data[0].resourcePath;
                    this.$refs.md.$img2Url(pos, path);
                }else{
                    this.$message.error("上传失败");
                }
            })
         },
        change22(value, render){
            // render 为 markdown 解析后的结果
            this.editForm.contentHtml = render;
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            this.$http.postHead('hgz/notice/queryListWithPage',this.query).then(res =>{
                if(res.status){
                   this.tableData = res.data.list;
                }
            })
        },
        // 触发搜索按钮
        addBanner() {
            this.title = "添加公告";
            this.editVisible = true;
            this.editForm = {
                title:'',
                isHide:"1",
                content:''
            }
        },
        // 编辑操作
        handleEdit(index, row) {
            console.log(row)
            this.title = "修改公告";
            this.editVisible = true;
            this.editForm = {
                id:row.id,
                title:row.title,
                isHide:row.isHide==1?'1':'2',
                content:row.content
            };
        },
        handleDelete(index, row) {
            this.$confirm('此操作将永久删除该公告, 是否继续?', '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let id = row.id;
                this.$http.postHead(`hgz/notice/deleteById/${id}`)
                .then(res =>{
                    if(res.status){
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getData();
                    }else{
                        this.$message({
                            type: 'error',
                            message: res.message
                        });   
                    }
                }).catch(() =>{
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    }); 
                })

            });

        },
        // 保存编辑
        saveEdit() {
             this.editForm.content = this.editForm.contentHtml;
             this.$http.postJson('hgz/notice/save',this.editForm).then(res =>{
                if(res.status){
                   this.$message.success("操作成功");
                   this.editVisible = false;
                   this.getData();
                }else{
                   this.$message.error(res.message);
                }
            })
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageNum', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.orange {
    color: orange;
}
.mid{
    font-size: 16px;;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
